/**
 * Copyright (C) 2013-2014 the original author or authors.
 */
package com.foreveross.bsl.mam.rbac.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;

import com.foreveross.bsl.common.utils.web.model.PageMustache;
import com.foreveross.bsl.mam.rbac.application.PrivilegeApplication;
import com.foreveross.bsl.mam.rbac.application.vo.PrivilegeVO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.foreveross.bsl.mam.rbac.application.OperationApplication;
import com.foreveross.bsl.mam.rbac.application.vo.OperationVO;

/**
 * <BR>
 * [功能详细描述]
 * 
 * @author Amberlo
 * @version [bsl-web , 2013-6-24]
 */
@Controller
@RequestMapping("/rbac/operation")
public class OperationController extends RbacBaseController{
 
	@Inject
	private OperationApplication operationApplication;

    @Inject
    private PrivilegeApplication privilegeApplication;

	@ResponseBody
	@RequestMapping("/add")
	public String add(String operationName,String operationCode) {
		OperationVO operation = operationApplication.getOperationByName(operationName);
		if(operation!=null) return "same";//重名校验
		operation=new OperationVO();
		operation.setName(operationName);
		operation.setCode(operationCode);
		operationApplication.saveEntity(operation);
		return "success";
	}
	
	@ResponseBody
	@RequestMapping("/get")
	public OperationVO get(@RequestParam String id) {
		return operationApplication.getEntity(id);
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public Map<String, Object> delete(String[] ids) {
		Map<String, Object> result = new HashMap<String, Object>();
		if (ids != null) {
			for (int i = 0; i < ids.length; i++) {
				OperationVO op = operationApplication.getEntity(ids[i]);
                List<PrivilegeVO> privileges = privilegeApplication.getPrivilegesByOperId(op.getId());
                if(privileges.size() != 0){
                    result.put("error","");
                }else{
                    if(op!=null){
                        operationApplication.deleteEntity(op);
                        result.put("result", "success");
                    }
                }
			}
		}

		return result;
	}

	
	@RequestMapping("/operation-view")
	public String listView(Model model,@RequestParam(value = "page", defaultValue = "1") int pageNumber,
			@RequestParam(value = "page.size", defaultValue = "10") int pageSize){
		Pageable pageable = new PageRequest(pageNumber-1, pageSize);
		Page<OperationVO> list = operationApplication.findByPageable(pageable);
		model.addAttribute("list", list);
		model.addAttribute("page", new PageMustache(list, 10));
		return "rbac/rbac-operation";
	}
	
	
	@RequestMapping("/editOperation")
	public String updateOperation(OperationVO operationVO) {
		operationApplication.updateEntity(operationVO);
		return "redirect:operation-view";
	}

    @ResponseBody
    @RequestMapping("/isExist")
    public Map<String,String> isExist(@RequestParam String name) {
        Map<String,String> result = new HashMap<String, String>();
        OperationVO operation = operationApplication.getOperationByName(name);
        if(operation != null){
            result.put("error","该操作名已存在,请重新输入");
        }else{
            result.put("success","");
        }
        return result;
    }

}
